package com.sanjiahantang.youjiaclient.module.evaluate.presenters;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyGridView;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.StoresAppraiseInitResponse;
import com.sanjiahantang.youjiaclient.module.evaluate.models.IEvaluateModel;
import com.sanjiahantang.youjiaclient.module.evaluate.models.impl.EvaluateModel;
import com.sanjiahantang.youjiaclient.module.evaluate.views.MeEvaluateView;
import com.sanjiahantang.youjiaclient.module.evaluate.views.StoreEvaluateView;
import com.sanjiahantang.youjiaclient.module.store.beans.StoreDetailBean;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yan on 2018/1/5.
 */

public class StoreEvaluatePresenter extends BaseMvpPresenter<StoreEvaluateView> {
    private QuickAdapter storeEvaluateAdapter;

    private IEvaluateModel evaluateModel;

    public StoreEvaluatePresenter() {
        evaluateModel = new EvaluateModel();
    }

    public void loadStoreEva(Context context, String roleId) {

        mView.showProgress();

        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PAGE, 0);
        param.put(KEY_PAGE_SIZE, DEFAULT_PAGE_SIZE);
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_ROLEPHONE, roleId);
        param.put(KEY_PARAM, map);
        evaluateModel.loadStoreEvaData(context, param, new StoreEvaCallBack());

    }


    /**
     * 初始化适配器
     *
     * @param context
     */
    public void initAdapter(Context context, StoreEvaluateView.OnListLoadListener listener) {

        storeEvaluateAdapter = new QuickAdapter<StoresAppraiseInitResponse.Item>(context, R.layout.item_eva_store_unlogin) {
            @Override
            protected void convert(BaseAdapterHelper helper, StoresAppraiseInitResponse.Item item) {
                if (item.getRolePhone() != null){
                    helper.setText(R.id.txtPhone, item.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
                }
                ((TextView) helper.getView(R.id.txtContent)).setText(item.getPlace() + "");
                if (item.getCreateTime() != null && !item.getCreateTime().isEmpty()) {
                    helper.setText(R.id.txtCreateTime, item.getCreateTime().substring(0, "yyyy-MM-dd".length()));
                }
                RatingBar ratingBar = helper.getView(R.id.ratingBar);
                try {
                    ratingBar.setRating(item.getUserNumber());
                    ratingBar.setIsIndicator(true);
                } catch (NumberFormatException e) {
                    Log.e("rating", e.getMessage());
                }
                MyGridView myGridView = helper.getView(R.id.gvImageList);
                myGridView.setAdapter(new com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter<StoreDetailBean.Icon>(context, R.layout.item_image, getImageList(item.getAppraiseUrl())) {
                    @Override
                    protected void convert(com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper helper, final StoreDetailBean.Icon item) {
                        ImageView iv = helper.getView(R.id.image);
                        Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_rect_bg).into(iv);
                        iv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Utils.showPicDialog((Activity) context,FileUtils.rePath(item.getUrl()),R.drawable.i_rect_bg);
                            }
                        });
                    }
                });
            }
        };
        listener.initContainers(storeEvaluateAdapter);
    }

    /**
     * 截取ImageUrl放到list集合里
     *
     * @param appraiseUrl
     * @return
     */
    private List<StoreDetailBean.Icon> getImageList(String appraiseUrl) {
        String urlListString = appraiseUrl;
        if (urlListString == null || urlListString.isEmpty()) {
            return null;
        }
        List<StoreDetailBean.Icon> list = new ArrayList<>();

        if (urlListString.contains(",")) {
            String[] strs = urlListString.split(",");
            for (int i = 0; i < strs.length; i++) {
                StoreDetailBean.Icon icon = new StoreDetailBean.Icon();
                icon.setUrl(strs[i]);
                list.add(icon);
            }
        } else {
            StoreDetailBean.Icon icon = new StoreDetailBean.Icon();
            icon.setUrl(urlListString);
            list.add(icon);
        }

        return list;
    }

    /**
     * 体验店评价回调类
     */
    private class StoreEvaCallBack implements CallBack<StoresAppraiseInitResponse> {

        @Override
        public void onSuccess(StoresAppraiseInitResponse response, Context context, Object tag) {
            mView.hideProgress();
            if (response.getCode() == RESPONSE_CODE_SUC) {
                storeEvaluateAdapter.clear();
                storeEvaluateAdapter.addAll(response.getData().getList());

            }

        }

        @Override
        public void onFailure(String message) {
            mView.hideProgress();
        }


    }
}
